package com.logic.modular.game.battle.process.skillEffectHandle.impl;

import com.logic.modular.game.battle.panel.attribute.domain.EffectStatus;
import com.logic.modular.game.battle.panel.attribute.domain.PlayerHeroAttribute;
import com.logic.modular.game.battle.panel.skillEffect.PvpSkillEffectServiceImpl;
import com.logic.modular.game.battle.panel.skillEffect.domain.BaseTrigger;
import com.logic.modular.game.battle.process.skillEffectHandle.base.BaseSkillEffectStrategy;
import com.logic.modular.game.battle.panel.skillEffect.PvpSkillEffect;
import com.comment.pojo.domain.logic.core.CoreSkillEffectType;
import com.logic.modular.gameCore.match.battle.service.impl.CoreSkillEffectInfoServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class EffectCategory {
    @Component
    public static class BaseEffectCategory extends BaseSkillEffectStrategy {
        @Resource
        public PvpSkillEffectServiceImpl pvpSkillEffectService;

        @Override
        public PvpSkillEffect applyEffect(PvpSkillEffect pvpSkillEffect) {
            effectHandle2.tryTriggerJudgment(pvpSkillEffect);
            effectHandle2.logTriggerFailReasons(pvpSkillEffect);

            return apply(pvpSkillEffect);
        }

        @Override
        public PvpSkillEffect apply(PvpSkillEffect pvpSkillEffect) {
            return pvpSkillEffect;
        }
    }

    // 47	净化	移除有害效果，包括控制技能（混乱，缴械，技穷，暴走，挑衅）dot伤害（溃逃，燃烧，妖术，恐慌）			[23,24,25,26,54,28,29,30,31]	10
    @Slf4j
    @Component
    public static class Effect_47 extends BaseEffectCategory {
        @Resource
        private CoreSkillEffectInfoServiceImpl coreSkillEffectTypeService;

        @Override
        public PvpSkillEffect apply(PvpSkillEffect pvpSkillEffect) {
            if (pvpSkillEffect.status.getIsTrigger() != 1) {
                attributeService.refreshPanel(pvpSkillEffect);
                return pvpSkillEffect;
            }
            List<CoreSkillEffectType> coreSkillEffectTypes = coreSkillEffectTypeService.listCache();
            List<Long> targetEffectTypeIdList = coreSkillEffectTypes.stream()
                    .filter(item -> item.getBuffType().equals(20) || item.getBuffType().equals(21))
                    .map(CoreSkillEffectType::getId)
                    .collect(Collectors.toList());


            String battleUUID = pvpSkillEffect.getBattleUUID();
            Long battleId = pvpSkillEffect.getBattleId();

            Long sourcePlayerHeroId = pvpSkillEffect.getSourcePlayerHeroId();
            PlayerHeroAttribute sourceAttribute = attributeService.get(battleUUID, sourcePlayerHeroId);

            Long targetPlayerHeroId = pvpSkillEffect.getTargetPlayerHeroId();
            Integer trigger6 = pvpSkillEffect.getBaseTrigger().getTrigger6();
            List<PlayerHeroAttribute> targetHeroes =
                    releaseSkillService2.getTargetHeroes(battleUUID, targetPlayerHeroId, trigger6);

            List<Object> back = new ArrayList<>();
            for (PlayerHeroAttribute targetAttribute : targetHeroes) {
                Map<Long, EffectStatus> status = targetAttribute.getStatus();
                status.forEach((coreSkillEffectTypeId, effectStatus) -> {
                    if (targetEffectTypeIdList.contains(coreSkillEffectTypeId)) {
                        status.remove(coreSkillEffectTypeId);
                    }
                });
                targetAttribute.setStatus(status);
                attributeService.saveCache(targetAttribute);

                List<PvpSkillEffect> targetSkillEffectList = pvpSkillEffectService.list(battleId, targetAttribute.getId());
                List<Map<String, Object>> collect = targetSkillEffectList.stream()
                        .filter(item -> targetEffectTypeIdList.contains(item.getCoreSkillEffectTypeId()))
                        .map(item -> {
                            pvpSkillEffectService.remove(item);
                            Map<String, Object> map = new HashMap<>();
                            map.put("来源武将", item.getSourcePlayerHeroId());
                            map.put("来源武将名字", item.getSourcePlayerHeroName());
                            map.put("来源技能id", item.getSourceCoreSkillId());
                            map.put("目标武将名字", item.getTargetPlayerHeroName());
                            map.put("目标武将", item.getTargetPlayerHeroId());
                            map.put("效果类型", item.getCoreSkillEffectTypeId());
                            return map;
                        }).collect(Collectors.toList());
                back.addAll(collect);
            }
            Map<String, Object> commentInfo = new HashMap<>();
            commentInfo.put("移除效果", back);
            pvpSkillEffect.skillEffectTriggerInfo.setCommonInfo(commentInfo);
            return pvpSkillEffect;
        }
    }

    // 63	跳过1回合准备	部分主动战法需要准备回合，这个可以跳过1回合的准备				10
    @Component
    public static class Effect_63 extends BaseEffectCategory {
        @Resource
        private CoreSkillEffectInfoServiceImpl coreSkillEffectTypeService;

        @Override
        public PvpSkillEffect applyEffect(PvpSkillEffect pvpSkillEffect) {
            if (pvpSkillEffect.status.getIsTrigger() != 1) {
                attributeService.refreshPanel(pvpSkillEffect);
                return pvpSkillEffect;
            }

            String battleUUID = pvpSkillEffect.getBattleUUID();
            Long battleId = pvpSkillEffect.getBattleId();

            Long sourcePlayerHeroId = pvpSkillEffect.getSourcePlayerHeroId();
            PlayerHeroAttribute sourceAttribute = attributeService.get(battleUUID, sourcePlayerHeroId);

            Long targetPlayerHeroId = pvpSkillEffect.getTargetPlayerHeroId();
            Integer trigger6 = pvpSkillEffect.getBaseTrigger().getTrigger6();
            List<PlayerHeroAttribute> targetHeroes =
                    releaseSkillService2.getTargetHeroes(battleUUID, targetPlayerHeroId, trigger6);

            List<Object> back = new ArrayList<>();
            for (PlayerHeroAttribute targetAttribute : targetHeroes) {
                List<PvpSkillEffect> targetSkillEffectList = pvpSkillEffectService.list(battleId, targetAttribute.getId());
                List<Map<String, Object>> collect = targetSkillEffectList.stream()
                        .filter(item -> item.getBaseTrigger().getPrepare() > 1)
                        .map(item -> {
                            BaseTrigger baseTrigger = item.getBaseTrigger();
                            Integer i = baseTrigger.getPrepare() - 1;
                            baseTrigger.setPrepare(i);
                            item.setBaseTrigger(baseTrigger);
                            pvpSkillEffectService.saveCache(item);

                            Map<String, Object> map = new HashMap<>();
                            map.put("来源武将", item.getSourcePlayerHeroId());
                            map.put("来源武将名字", item.getSourcePlayerHeroName());
                            map.put("来源技能id", item.getSourceCoreSkillId());
                            map.put("目标武将名字", item.getTargetPlayerHeroName());
                            map.put("目标武将", item.getTargetPlayerHeroId());
                            map.put("效果类型", item.getCoreSkillEffectTypeId());
                            return map;
                        }).collect(Collectors.toList());
                back.addAll(collect);
            }
            Map<String, Object> commentInfo = new HashMap<>();
            commentInfo.put("跳过一回合准备的技能效果", back);
            pvpSkillEffect.skillEffectTriggerInfo.setCommonInfo(commentInfo);
            return pvpSkillEffect;
        }
    }

}
